/**
 * @author		Sergey Suzdalskiy
 * @e-mail:		sergey.suzdalskiy@gmail.com
 */
package ua.kiev.gcore.ui.tooltip
{
	import flash.display.DisplayObject;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	public class ToolTip extends Sprite implements IToolTip
	{
		protected var target:DisplayObject;
		private var _isOnStage:Boolean;
		public var isEnabled:Boolean;
		public var size:Point;
		public var id:String;
		
		public function ToolTip(id:String, target:DisplayObject, toolTip:Sprite, size:Point)
		{
			super();
			this.id = id;
			this.target = target;
			this.size = size;
			addChild(toolTip);
			this.mouseChildren = false;
			this.mouseEnabled = false;
			toolTip.mouseChildren = false;
			toolTip.mouseEnabled = false;
			
			target.addEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
			target.addEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
			
		}
		public function set isOnStage(value:Boolean):void{
			_isOnStage = value;
		}
		public function get isOnStage():Boolean{
			return _isOnStage;
		}
		public function enterFream():void{
			
		}
		
		public function destroy():void{
			if(target){				
					
				if(target.hasEventListener(MouseEvent.MOUSE_OVER)){
					target.removeEventListener(MouseEvent.MOUSE_OVER, mouseOverHandler);
				}
				if(target.hasEventListener(MouseEvent.MOUSE_OUT)){
					target.removeEventListener(MouseEvent.MOUSE_OUT, mouseOutHandler);
				}
			}
			if(isEnabled){
				hide();
				dispatchEvent(new ToolTipEvent(ToolTipEvent.TOOL_TIP_HIDE));
			}
		}
		protected function show():void{
			
		}
		protected function hide():void{
			
		}
		private function mouseOverHandler(event:Event):void{
			if(isEnabled){
				show();
				dispatchEvent(new ToolTipEvent(ToolTipEvent.TOOL_TIP_SHOW));
			}
		}
		
		private function mouseOutHandler(event:Event):void{
			if(isEnabled){
				hide();
				dispatchEvent(new ToolTipEvent(ToolTipEvent.TOOL_TIP_HIDE));
			}
		}
	}
}